<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<%@ include file="/WEB-INF/views/include/easyui.jsp"%>
<script src="<%=path %>/static/plugins/easyui/jeasyui-extensions/jeasyui.extensions.form.js" type="text/javascript"></script>
</head>
<body>
	<table id="privateEmailSeting"></table>  
	
	<div id="addOrUpdate" class="easyui-dialog" closed="true" style="width:400px;height:150px;" 
	data-options="iconCls:'icon-save',resizable:true,modal:true,title:'addOrUpdate'">
		<form id="addOrUpdateForm">
			<table width="100%" height="70%">
				<tr>
					<td>邮箱账号:&nbsp;<input type="text" id="emailCode" name="emailCode" style="width:300px;"/></td>
				</tr>
				<tr>
					<td>&nbsp;授&nbsp;权&nbsp;码:&nbsp;<input type="password" id="emailPassword" name="emailPassword" style="width:300px;"/></td>
				</tr>
			</table>
		</form>
		<div id="quy-buttons" style="text-align: center;border-top:1px solid #ccc;padding:10px;">
			<a onclick="addOrUpdate()" class="easyui-linkbutton" iconCls="icon-ok" >提交</a>
			<a style="margin-left: 10px;" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#addOrUpdate').dialog('close');">取消</a>
		</div>
	</div>
</body>

<script>
var index;
$('#privateEmailSeting').datagrid({    
    url:'<%=path%>/emailSeting/initEmailSeting',    
    columns:[[ 
		{field:'id',hidden:true},
		{field:'defult',hidden:true},
        {field:'emailCode',title:'邮箱账号',width:200},    
        {field:'emailPassword',title:'授权码',width:180},    
        {field:'see',title:'默认发送邮箱',width:120,align:'center'}    
    ]],
    fit:true,
    pagination:true,//分页控件 
    rownumbers:true,//行号 
    autoEditing: true,
    striped:true,
    singleSelect:true,
    onLoadSuccess:function(data){
    	var rows=data.rows;
    	for(var i=0;i<rows.length;i++){
    		var rowIndex=$('#privateEmailSeting').datagrid('getRowIndex',rows[i]);
    		if(rows[i].defult=='1'){
    			$('#privateEmailSeting').datagrid('updateRow',{
    				index: rowIndex,
    				row: {
    					see: '<span style="color:#55AA55;">当前设置</span>',
    					emailPassword:'*******'
    				}
    			});
    		}else{
    			$('#privateEmailSeting').datagrid('updateRow',{
    				index: rowIndex,
    				row: {
    					see: '<a style="color:blue" href="javascript:setingMailDefult()">设为默认</a>',
    					emailPassword:'*******'
    				}
    			});
    		}
    	}
    },
    toolbar:[{
		iconCls: 'icon-add',
		text:'添加',
		handler: function(){
			index=1;
			$('#addOrUpdate').dialog('open');
		}
	},'-',{
		iconCls: 'icon-edit',
		text:'修改',
		handler: function(){
			index=0;
			var row= $('#privateEmailSeting').datagrid('getSelected');
			if(row!=null){
				$('#addOrUpdate').dialog('open');
				$('#emailCode').val(row.emailCode);
				$('#emailPassword').val(row.emailPassword);
			}
		}
	},'-',{
		iconCls: 'icon-remove',
		text:'删除',
		handler: function(){
			remove();
		}
	}]
});  


//取消and设置默认发送邮箱
function setingMailDefult(){
	var row= $('#privateEmailSeting').datagrid('getSelected');
	$.ajax({
		url:'<%=path%>/emailSeting/setDefult',
		type:'post',
		data:{'ID':row.id},
		async:false,
		success:function(data){
			 var r = eval('(' + data + ')');
			 if(r.success){
				 /* $.messager.show({
						msg : r.msg,
						title : '提示'
					}); */
				 $('#privateEmailSeting').datagrid('reload');
			 }else{
				 $.messager.show({
						msg : r.msg,
						title : '提示'
				});
			 }
		}
	});
}


function addOrUpdate(){
	var ID;
	if(index=='0'){
		var row= $('#privateEmailSeting').datagrid('getSelected');
		ID=row.id;
	}
	$('#addOrUpdateForm').form('submit', {   
		 url:'<%=path%>/emailSeting/addOrUpdateEmailSeting',  
		 onSubmit: function(param){
			 param.ID=ID; 
			 param.index=index;
		 }, 
		 success: function(data){    
			 var r = eval('(' + data + ')');
			 if(r.success){
				 $.messager.show({
						msg : r.msg,
						title : '提示'
					});
				 $('#privateEmailSeting').datagrid('reload');
				 $('#addOrUpdate').dialog('close');
				 $('#addOrUpdateForm')[0].reset();
			 }else{
				 $.messager.show({
						msg : r.msg,
						title : '提示'
				});
			 }
		 }    
	});
} 

function remove(){
	var row= $('#privateEmailSeting').datagrid('getSelected');
	if(row!=null){
		$.ajax({
			 url:'<%=path%>/emailSeting/remove',
			 type:'post',
			 data:{'ID':row.id},
			 async:false,
			 success:function(data){
				 var r = eval('(' + data + ')');
				 if(r.success){
					 $.messager.show({
	 						msg : r.msg,
	 						title : '提示'
	 					});
					 $('#privateEmailSeting').datagrid('reload');
				 }else{
					 $.messager.show({
	 						msg : r.msg,
	 						title : '提示'
	 					});
				 }
			 }
		 });
	}
}
</script>
</html>